package webdev.common;

import java.io.UnsupportedEncodingException;
import java.math.BigInteger;
import java.net.URLEncoder;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import org.jsoup.Jsoup;

import webdev.dto.UserDTO;
import webdev.imgloader.ImageLoader;
import webdev.imgloader.ImageLoaderCircle;
import webdev.imgloader.ImageLoaderLocal;
import webdev.models.User;
import android.util.Base64;
import android.widget.Toast;

public class Common {

	public static UserDTO currentUser;
	
	// OWNER USER INFOMATION
	public static User USER_INFOMATION;
	// This is used to load image normal in application
	public static ImageLoader IMAGE_LOADER_YOULOOK;
	// This is used to load circle image in application
	public static ImageLoaderCircle IMAGE_LOADER_CIRCLE_YOULOOK;
	// This is used to load image normal in application
	public static ImageLoaderLocal IMAGE_LOADER_LOCAL_YOULOOK;
//	public static ImageLoaderSmallLarge IMAGE_LOADER_SMALL_LARGE_YOULOOK;
	public static boolean checkLogined = false;
	
    // The maximum number of item in once page for API
    public static int TAB_ForSearch = 0;
    //Facebook
    public static User userInfo;
//    public static String APP_ID = "453345281375610";
    public static String AUTHORIZE_STRING="";
    
    
    public static String authorization(String str) {
        String base64EncodedCredentials = Base64.encodeToString(str.getBytes(),
                Base64.NO_WRAP);
        return base64EncodedCredentials;
    }
    
    public static void showToast(Toast toast) {
    	if (!toast.getView().isShown()) {
			toast.show();
		}
    }

    public static String encodingUTF8(String value) {
        String encodedUrl;
        try {
            encodedUrl = URLEncoder.encode(value, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            encodedUrl = value;
        }
        return encodedUrl;
    }

    public static String getUrlImageFill(String cdn, String imageName, String id) {
    	if (("".equals(cdn))
    			|| ("".equals(id))
    			|| ("".equals(imageName))) {
    		return "";
    	}
    	StringBuffer imagePath = new StringBuffer(cdn);
    	imagePath.append("/upload/user-photos/");
    	imagePath.append(id);
    	imagePath.append("/fill/200-200/");
    	imagePath.append(imageName);
    	imagePath.append("?album_id=");
    	imagePath.append(id);
    	return imagePath.toString();
    }

    public static String getUrlImageThumb(String cdn, String imageName, String albumId) {
    	if (("".equals(cdn))
    			|| ("".equals(imageName))
    			|| ("".equals(albumId))) {
    		return "";
    	}
    	StringBuffer imagePath = new StringBuffer(cdn);
//		imagePath.append("/upload/gallery/thumbs/620-10000/");
    	imagePath.append("/upload/gallery/thumbs/700-500/");
//    	imagePath.append("/upload/gallery/thumbs/250-250/");
    	imagePath.append(imageName);
    	imagePath.append("?album_id=");
    	imagePath.append(albumId);
    	return imagePath.toString();
    }

    public static String encryptMD5(String input) {
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            byte[] messageDigest = md.digest(input.getBytes());
            BigInteger number = new BigInteger(1, messageDigest);
            String hashtext = number.toString(16);
            while (hashtext.length() < 32) {
                hashtext = "0" + hashtext;
            }
            return hashtext;
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException(e);
        }
    }
    
	public static String convertHtml2Text(String html) {
	    return Jsoup.parse(html).text();
	}
    
}
